<?php
/**
 * User:chendenan
 * Created On 2022/3/18 0018 21:41
 */
namespace app\admin\controller;
use app\common\business\Goods as GoodsBus;
class Goods extends AdminBase{
    public function index(){
        $data = [];
        $title = input("param.title","","trim");
        $time = input("param.time","","trim");
        if(!empty($title)){
            $data['title'] = $title;
        }
        if(!empty($time)){
            $data['create_time'] = explode(" - ",$time);
        }
        $goods = (new GoodsBus())->getLists($data,10);
        return view("",[
            "goods"=>$goods,
        ]);
    }
    public function add(){
        return view();
    }
    public function save(){
        if(!$this->request->isPost()){
            return show("参数不合法",config("status.error"));
        }
        $data = input("param.");
        $check = $this->request->checkToken('__token__');
        if(!$check){
            return show("非法请求",config("status.error"));
        }
        //预留validate校验
        //数据处理
        $data['category_path_id'] = $data['category_id'];
        $result = explode(",",$data['category_path_id']);
        $data['category_id'] = end($result);
//        try {
//            $res = (new GoodsBus())->insertData($data);
//        }catch (\Exception $e){
//            return show($e->getMessage(),config("status.error"));
//        }
        $res = (new GoodsBus())->insertData($data);
        if(!$res){
            return show("商品新增失败",config("status.error"));
        }
        return show("商品新增成功",config("status.success"));
    }
}